package com.fengwei.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class EnumUtil {
    public static void changeDes(Enum obj, String des) throws InvocationTargetException, IllegalAccessException {
        Method[] methods = RuntimeUtil.getMethods(obj);
        for (Method method : methods) {
            if (method.getName().equals("setpName")) {
                method.invoke(obj, des);
                break;
            }
        }
    }

    public static void changeCode(Enum obj, String code) throws InvocationTargetException, IllegalAccessException {
        Method[] methods = RuntimeUtil.getMethods(obj);
        for (Method method : methods) {
            if (method.getName().equals("setpCode")) {
                method.invoke(obj, code);
                break;
            }
        }
    }

    public static String getCode(Enum obj) throws InvocationTargetException, IllegalAccessException,NoSuchMethodException{
        Class clazz = obj.getDeclaringClass();
        Method toCode = clazz.getMethod("getpCode");
        String pCode = (String)toCode.invoke(obj);
        return pCode;
    }

    public static String getName(Enum obj) throws InvocationTargetException, IllegalAccessException,NoSuchMethodException{
        Class clazz = obj.getDeclaringClass();
        Method toName = clazz.getMethod("getpName");
        String pName = (String)toName.invoke(obj);
        return pName;
    }
}
